package com.example.module_home.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView.Adapter
import androidx.recyclerview.widget.RecyclerView.ViewHolder

open class CommonAdapter<D> (val layout:(D)->Pair<Int,Int>,val data:MutableList<D> = mutableListOf()):Adapter<CommonViewHolder>() {
    override fun getItemViewType(position: Int): Int =layout(data[position]).first
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CommonViewHolder =
        CommonViewHolder(DataBindingUtil.inflate(LayoutInflater.from(parent.context),viewType,parent,false))

    override fun onBindViewHolder(holder: CommonViewHolder, position: Int) {
       holder.binding.setVariable(layout(data[position]).second,data[position])
    }

    override fun getItemCount(): Int =data.size
    @SuppressLint("NotifyDataSetChanged")
    operator fun plusAssign(list: List<D>){
        val size=data.size
        data+=list
        notifyItemRangeInserted(size,list.size)
        notifyDataSetChanged()
    }
    @SuppressLint("NotifyDataSetChanged")
    fun clear(){
        data.clear()
        notifyItemRangeRemoved(0,data.size)
        notifyDataSetChanged()
    }

}

class CommonViewHolder(val binding:ViewDataBinding):ViewHolder(binding.root)
